/**************************************************
|	File:		OptionsMenuState.cpp
|	Author:		Benjamin Budian
|	Course:		SGD
|	Purpose:	Handles increasing and lowering sounds
**************************************************/

#include "OptionsMenuState.h"

//Wrappers
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/SGD_String.h"

//Locals
#include "BitmapFont.h"
#include "Game.h"
#include "MainMenuState.h"
#include "../TinyXML/tinyxml.h"


COptionsMenuState::COptionsMenuState(void){}

COptionsMenuState::~COptionsMenuState(void){}

COptionsMenuState* COptionsMenuState::GetInstance(){
	static COptionsMenuState s_Instance;
	return &s_Instance;
}

void COptionsMenuState::Enter(){
	m_nSFXID = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/sounds/bb_deaths.wav"));
	m_nBackgroundMusicID = CSGD_XAudio2::GetInstance()->MusicLoadSong(_T("resource/sounds/bb_background_music_demo.xwm"));

	if(Load() == false){
		m_nMusicVolume	= (int)(CSGD_XAudio2::GetInstance()->MusicGetMasterVolume() * 100);
		m_nSFXVolume	= (int)(CSGD_XAudio2::GetInstance()->SFXGetMasterVolume() * 100);
	}

	m_nCursor = SFX_VOLUME;
}

void COptionsMenuState::Exit(){
	Save();

	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(m_nSFXVolume/100.0f);
	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(m_nMusicVolume/100.0f);

	CSGD_XAudio2::GetInstance()->SFXUnloadSound(m_nSFXID);
	CSGD_XAudio2::GetInstance()->MusicUnloadSong(m_nBackgroundMusicID);
}

bool COptionsMenuState::Input(){

	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if(pDI->KeyPressed( DIK_DOWN ) == true){
		m_nCursor++;

		if(m_nCursor > MUSIC_VOLUME)
			m_nCursor = SFX_VOLUME;
	}
	if(pDI->KeyPressed( DIK_UP ) == true){
		m_nCursor--;

		if(m_nCursor < SFX_VOLUME)
			m_nCursor = MUSIC_VOLUME;
	}

	if(pDI->KeyPressed( DIK_LEFT ) == true){
		switch(m_nCursor){
		case MUSIC_VOLUME:
			m_nMusicVolume -= 5;

			if(m_nMusicVolume < 0)
				m_nMusicVolume = 0;

			CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(m_nMusicVolume/100.0f);

			break;
		case SFX_VOLUME:
			{
				m_nSFXVolume -= 5;

				if(m_nSFXVolume < 0)
					m_nSFXVolume = 0;

				CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(m_nSFXVolume/100.0f);

				CSGD_XAudio2::GetInstance()->SFXPlaySound(m_nSFXID);
			}
			break;
		}
	}
	if(pDI->KeyPressed( DIK_RIGHT ) == true){
		switch(m_nCursor){
		case MUSIC_VOLUME:
			m_nMusicVolume += 5;

			if(m_nMusicVolume > 100)
				m_nMusicVolume = 100;

			CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(m_nMusicVolume/100.0f);

			break;
		case SFX_VOLUME:
			m_nSFXVolume += 5;

			if(m_nSFXVolume > 100)
				m_nSFXVolume = 100;

			CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(m_nSFXVolume/100.0f);

			CSGD_XAudio2::GetInstance()->SFXPlaySound(m_nSFXID);
			break;
		}
	}


	if(pDI->KeyPressed( DIK_ESCAPE ) == true){
		CGame::GetInstance()->ChangeState( CMainMenuState::GetInstance() );
		return true;
	}

	if(m_nCursor == MUSIC_VOLUME){
		if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(m_nBackgroundMusicID) == false)
			CSGD_XAudio2::GetInstance()->MusicPlaySong(m_nBackgroundMusicID, true);
	}
	else
		if(CSGD_XAudio2::GetInstance()->MusicIsSongPlaying(m_nBackgroundMusicID) == true)
			CSGD_XAudio2::GetInstance()->MusicStopSong(m_nBackgroundMusicID);


	return true;
}

void COptionsMenuState::Update( float fElapsedTime ){}

void COptionsMenuState::Render(){

	CBitmapFont* pFont = CGame::GetInstance()->GetFont();

	int nCenterOffset = CGame::GetInstance()->GetScreenWidth()/2 - (strlen("options")*64)/2;

	pFont->Draw( _T("OPTIONS"), nCenterOffset, 100, 2.0f,D3DCOLOR_ARGB(255,255,255,255));

	nCenterOffset = CGame::GetInstance()->GetScreenWidth()/3 - (strlen("SFX Volume")*32)/2;
	pFont->Draw( _T("SFX Volume"), nCenterOffset, 300,1.0f,D3DCOLOR_ARGB(255,255,255,255));

	TOSTRINGSTREAM toss;
	toss << m_nSFXVolume;

	nCenterOffset = CGame::GetInstance()->GetScreenWidth()/3 * 2 - (toss.str().size()*32)/2;
	pFont->Draw( toss.str().c_str(), nCenterOffset, 300,1.0f,D3DCOLOR_ARGB(255,255,255,255));

	toss.str(_T(""));

	nCenterOffset = CGame::GetInstance()->GetScreenWidth()/3 - (strlen("Music Volume")*32)/2;
	pFont->Draw( _T("Music Volume"), nCenterOffset, 350,1.0f,D3DCOLOR_ARGB(255,255,255,255));

	toss << m_nMusicVolume;

	nCenterOffset = CGame::GetInstance()->GetScreenWidth()/3 * 2 - (toss.str().size()*32)/2;
	pFont->Draw( toss.str().c_str(), nCenterOffset, 350,1.0f,D3DCOLOR_ARGB(255,255,255,255));

	pFont->Draw( _T(">"), 100, 300 + (m_nCursor * 50), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
}

void COptionsMenuState::Save(){
	TiXmlDocument doc;

	TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "utf-8","");

	doc.LinkEndChild(pDecl);

	TiXmlElement* pRoot = new TiXmlElement("Audio");

	doc.LinkEndChild(pRoot);

	TiXmlElement* pVolume = new TiXmlElement("Volume");
	pVolume->SetAttribute("SFX",m_nSFXVolume);
	pVolume->SetAttribute("Music",m_nMusicVolume);

	pRoot->LinkEndChild(pVolume);

	doc.SaveFile("Volume.xml");
}

bool COptionsMenuState::Load(){
	TiXmlDocument doc;

	if(doc.LoadFile("Volume.xml") == false)
		return false;

	TiXmlElement* pRoot = doc.RootElement();

	if( pRoot == nullptr)
		return false;

	TiXmlElement* pVolume = pRoot->FirstChildElement("Volume");

	pVolume->Attribute("SFX",&m_nSFXVolume);
	pVolume->Attribute("Music",&m_nMusicVolume);

	CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(m_nSFXVolume/100.0f);
	CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(m_nMusicVolume/100.0f);

	return true;
}


